﻿//Sorting an array means to arrange its elements in increasing order. Write a program to sort an array.
//Use the "selection sort" algorithm: Find the smallest element, move it at the first position, find the
//smallest from the rest, move it at the second position, etc.


namespace SortingArray
{
    using System;

    public class SortingArray
    {
        public static void Main(string[] args)
        {
            Random randomGenerator = new Random();
            Console.Write("Enter array lengh: n = ");
            int arrayElementsCount = int.Parse(Console.ReadLine());
            int[] arrayOfElements = new int[arrayElementsCount];
            Console.WriteLine();
            Console.Write("Elements of array are:    ");

            for (int indexOfElement = 0; indexOfElement < arrayOfElements.Length; indexOfElement++)
            {
                arrayOfElements[indexOfElement] = randomGenerator.Next(100);
                Console.Write(arrayOfElements[indexOfElement] + " ");
            }

            Console.WriteLine();
            Console.Write("Elements in sorting order: ");
                                    
            for (int elemenIndex = 0; elemenIndex < arrayOfElements.Length; elemenIndex++)
            {
                int smallArrayElement = int.MaxValue;
                int tempVariable;   
                int smallElementIndex = 0;
                for (int i = elemenIndex; i < arrayOfElements.Length; i++)
                {
                    if (arrayOfElements[i] < smallArrayElement)
                    {
                        smallArrayElement = arrayOfElements[i];
                        smallElementIndex = i;
                    }
                }

                tempVariable = arrayOfElements[elemenIndex];
                arrayOfElements[elemenIndex] = smallArrayElement;
                arrayOfElements[smallElementIndex] = tempVariable;
                
                Console.Write(arrayOfElements[elemenIndex] + " ");
            }

            Console.WriteLine();            
        }
    }
}
